/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include "PLExceptionBase.h"

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

static exceptionList_t *exception_head_key = NULL;

static void exception_init(void) {
	if(exception_head_key == NULL) {
		plexception_t *_new;
		LIST_NODE_ALLOC(_new);
		initListHead(&(_new->list));
		exception_head_key = &(_new->list);
	}
}

int exception_push(const char *file, int line, const char *func, int errnum, const char *fmt, ...) {
	plexception_t *_new;
	exception_init();
	LIST_NODE_ALLOC(_new);
	_new->file   = file;
	_new->func   = func;
	_new->line   = line;
	_new->errnum = errnum;
	if(fmt == NULL) {
		_new->msg = NULL;
	} else {
		va_list ap;
		va_start(ap, fmt);
/* TODO
		vasprintf(&_new->msg, fmt, ap);*/
		_new->msg = (char *)fmt;
		va_end(ap);
	}
	list_add(&_new->list, exception_head_key);
	return 0;
}

bool exception_empty(void) {
	exception_init();
	return list_empty(exception_head_key);
}

int exception_errno(void) {
	if(exception_empty()) {
		return 0;
	}
	exceptionList_t *pos = exception_head_key->prev;
	plexception_t *e = list_entry(pos, plexception_t, list);
	return e->errnum;
}

void exception_clear(void) {
	if(!exception_head_key) {
		return;
	}
	exceptionList_t *pos, *tmp;
	list_for_each_safe(pos, tmp, exception_head_key) {
		list_del(pos);
		plexception_t *e = list_entry(pos, plexception_t, list);
		if(e->msg) {
			free(e->msg);
		}
		free(e);
	}
}

typedef struct {
	exceptionList_t list;
	jmp_buf env;
} tryenv_t;

static exceptionList_t *tryenv_head_key = 0;

static void tryenv_init(void) {
	exceptionList_t *head = tryenv_head_key;
	if(!head) {
		tryenv_t *_new;
		LIST_NODE_ALLOC(_new);
		initListHead(&(_new->list));
		tryenv_head_key = &(_new->list);
	}
}

static bool tryenv_empty(void) {
	tryenv_init();
	return list_empty(tryenv_head_key);
}

static void tryenv_default_handler(void) {
    exceptionList_t *ex, *tmp;
    list_for_each_safe(ex, tmp, exception_head_key) {
        plexception_t *e = list_entry(ex, plexception_t, list);
        printf("\nException: %s %s:%d", e->file, e->func, e->line);
    }
	abort();
}

void tryenv_push(jmp_buf *env, int ret) {
	if(ret != 0) {
		return;
	}
	tryenv_init();
	tryenv_t *_new = 0;
	LIST_NODE_ALLOC(_new);
	memcpy(&_new->env, env, sizeof(jmp_buf));
	list_add(&_new->list, tryenv_head_key);
}

void tryenv_pop(void) {
	if(tryenv_empty()) {
		return;
	}
	exceptionList_t *pos = tryenv_head_key->next;
	tryenv_t *env = list_entry(pos, tryenv_t, list);
	list_del(pos);
	free(env);
}

void tryenv_jmp(void) {
	if(tryenv_empty()) {
		tryenv_default_handler();
	}
	exceptionList_t *pos = tryenv_head_key->next;
	tryenv_t *env = list_entry(pos, tryenv_t, list);
	jmp_buf buf;
	memcpy(&buf, &env->env, sizeof(jmp_buf));
	list_del(pos);
	free(env);
	longjmp(buf, 1);
}
